function qew_close() {
	var html = document.getElementsByTagName('html')[0];
	var qew_frame = document.getElementById('qew_frame');
	var qew_wrapper = document.getElementById('qew_wrapper');
	html.className = html.className.replace(/\bqew_open\b/, '');
	qew_frame.src = '/a/a/quick_edit_window_placeholder.html';
	qew_wrapper.style.bottom = '';
	qew_wrapper.style.height = '';
	//fix for IE7
	var wrapper = document.getElementById('wrapper');
	wrapper.style.bottom = 'auto';
	//end fix for IE7
	window.qew_is_open = false;
	$("#QuickEditLink").html("Enable Quick Edit");
	$("#QuickEditLink").attr('onclick', '').click(new Function('qew_open();'));
}


function qew_open() {
	window.qew_is_open = true;
	$("#QuickEditLink").html("Disable Quick Edit");
	$("#QuickEditLink").attr('onclick', '').click(new Function('qew_close();'));
	var html = document.getElementsByTagName('html')[0];
	var qew_frame = document.getElementById('qew_frame');
	html.className = html.className.replace(/\bqew_open\b/, '') + ' qew_open';
	qew_frame.src = '/a/a/quick_edit_window_placeholder.html';
	qew_resize();
}

function qew_load(evt, element, id) {	
	if (window.qew_Previous_Element) {
		window.qew_Previous_Element.className = window.qew_Previous_Element.className.replace(/\bselected\b/, '');
	}

	if (window.qew_is_open) {
		var qew_frame = document.getElementById('qew_frame');
		qew_frame.src = detailPage + id + '&Is_Quick_Edit_Window=Y';
		
		//set the id on the divider here instead of via a cookie
		v$('quick_edit_window_uniqueid').innerHTML=id;
		
		var parent = element;
		while (parent && parent.nodeName.toLowerCase() != 'tr') {
			parent = parent.parentNode;
		}
		
		if (parent) {
			window.qew_Previous_Element = parent;
			parent.className += ' selected';
		}
	}
}

function qew_resize() {
	var minimum = 180;
	var maximum = 120;

	if (window.qew_is_open) {
		var divider = document.getElementById('qew_divider');
		var wrapper = document.getElementById('wrapper');
		var qew_wrapper = document.getElementById('qew_wrapper');
	
		var docHeight = document.documentElement.clientHeight;
		var divider_placement = vPlacement(divider);
		if (divider_placement.top < minimum) divider_placement.top = minimum;
		if (docHeight - maximum < divider_placement.top) divider_placement.top = docHeight - maximum;
		var bottomPercent = parseInt(divider_placement.top / docHeight * 10000) / 100;
		var topPercent = 100 - bottomPercent;
		wrapper.style.bottom = topPercent + '%';
		divider.style.top = bottomPercent + '%';
		qew_wrapper.style.top = bottomPercent + '%';
		qew_wrapper.style.height = topPercent + '%';
		qew_ie6_resize();
	}
}

function qew_ie6_resize() {
}

function mouseOffset(event) {
	if (event.pageX >= 0 || event.pageX < 0) {
		return [event.pageX, event.pageY];
	} else if (event.clientX >= 0 || event.clientX < 0) {
		var offset = scrollOffset();
		return [event.clientX + offset[0], event.clientY + offset[1]];
	}
}

function scrollOffset() {
	if (window.pageXOffset) {
		return [window.pageXOffset, window.pageYOffset];
	} else if (document.documentElement) {
		return [
				document.body.scrollLeft + document.documentElement.scrollLeft, 
				document.body.scrollTop + document.documentElement.scrollTop];
	} else if (document.body.scrollLeft >= 0) {
		return [document.body.scrollLeft, document.body.scrollTop];
	} else {
		return [0, 0];
	}
}
function qew_mousedown(evt) {
	var event = window.event || evt;
	var mousePosition = mouseOffset(event);
	var divider = document.getElementById('qew_divider');
	var frame = document.getElementById('qew_frame');

	window.qew_offsetY = ((event.offsetY) ? event.offsetY : event.layerY) || 0;

	disableHighlighting();

	document.onmousemove = qew_mousemove;
	document.onmouseup = qew_mouseup;

	frame.contentWindow.document.onmousemove = qew_mousemove;
	frame.contentWindow.document.onmouseup = qew_mouseup;

	document.body.style['cursor'] = 'n-resize';

	if (frame.contentWindow.document.body) {
		frame.contentWindow.document.body.style['cursor'] = 'n-resize';
	}
}

function qew_mouseup(evt) {
	enableHighlighting();

	var frame = document.getElementById('qew_frame');
	document.onmousemove = null;
	document.onmouseup = null;
	frame.contentWindow.document.onmousemove = null;
	frame.contentWindow.document.onmouseup = null;

	document.body.style['cursor'] = '';

	if (frame.contentWindow.document.body) {
		frame.contentWindow.document.body.style['cursor'] = '';
	}
	
	qew_resize();
}

function qew_mousemove(evt) {
	var event = window.event || evt;
	var divider = document.getElementById('qew_divider');
	var frame = document.getElementById('qew_frame');
	var top = 0;

	if (!event) {
		event = frame.contentWindow.window.event;
		top = vPlacement(frame).top;
	}
	else if (!window.event) {
		if (document != event.currentTarget) {
			top = vPlacement(frame).top;
		}
	}
	event.cancelBubble = true;
	if (event.stopPropagation) event.stopPropagation();

	var mousePosition = mouseOffset(event);
	divider.style.top = top + mousePosition[1] - window.qew_offsetY + 'px';
	
	qew_resize();
}

function disableHighlighting() {
	//if the browser is IE4+
	document.onselectstart=new Function ('return false');

	//if the browser is NS6
	if (window.sidebar){
		document.onmousedown=disableText;
		document.onclick=enableText;
	}
}

function enableHighlighting() {
	//if the browser is IE4+
	document.onselectstart=null;

	//if the browser is NS6
	if (window.sidebar){
		document.onmousedown=null;
		document.onclick=null;
	}
}

function disableText(e){
	return false;
}
function enableText(){
	return true;
}
